home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP13.ZIP / PATRON / ICLASSF.CPP < prev    next >
C/C++ Source or Header  |  1993-06-27  |  5KB  |  205 lines

  1. /*
  2.  * ICLASSF.CPP
  3.  *
  4.  * Implementation of an IClassFactory interface for Patron to enable
  5.  * linking to its embedded objects.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "patron.h"
  18.  
  19.  
  20.  
  21. /*
  22.  * CLinkClassFactory::CLinkClassFactory
  23.  * CLinkClassFactory::~CLinkClassFactory
  24.  *
  25.  * Constructor Parameters:
  26.  *  pFR             LPCLinkFrame that can create documents.
  27.  */
  28.  
  29. CLinkClassFactory::CLinkClassFactory(LPCPatronFrame pFR)
  30.     {
  31.     m_cRef=0L;
  32.     m_pFR=pFR;
  33.     return;
  34.     }
  35.  
  36.  
  37. CLinkClassFactory::~CLinkClassFactory(void)
  38.     {
  39.     return;
  40.     }
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * CLinkClassFactory::QueryInterface
  49.  * CLinkClassFactory::AddRef
  50.  * CLinkClassFactory::Release
  51.  */
  52.  
  53. STDMETHODIMP CLinkClassFactory::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  54.     {
  55.     *ppv=NULL;
  56.  
  57.     //Any interface on this object is the object pointer.
  58.     if (IsEqualIID(riid, IID_IUnknown) || IsEqualIID(riid, IID_IClassFactory))
  59.         *ppv=(LPVOID)this;
  60.  
  61.     /*
  62.      * If we actually assign an interface to ppv we need to AddRef it
  63.      * since we're returning a new pointer.
  64.      */
  65.     if (NULL!=*ppv)
  66.         {
  67.         ((LPUNKNOWN)*ppv)->AddRef();
  68.         return NOERROR;
  69.         }
  70.  
  71.     return ResultFromScode(E_NOINTERFACE);
  72.     }
  73.  
  74.  
  75. STDMETHODIMP_(ULONG) CLinkClassFactory::AddRef(void)
  76.     {
  77.     return ++m_cRef;
  78.     }
  79.  
  80.  
  81. STDMETHODIMP_(ULONG) CLinkClassFactory::Release(void)
  82.     {
  83.     ULONG           cRefT;
  84.  
  85.     cRefT=--m_cRef;
  86.  
  87.     if (0L==m_cRef)
  88.         delete this;
  89.  
  90.     return cRefT;
  91.     }
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. /*
  100.  * CLinkClassFactory::CreateInstance
  101.  *
  102.  * Purpose:
  103.  *  Instantiates a Figure object that supports embedding.
  104.  *
  105.  * Parameters:
  106.  *  punkOuter       LPUNKNOWN to the controlling IUnknown if we are
  107.  *                  being used in an aggregation.
  108.  *  riid            REFIID identifying the interface the caller desires
  109.  *                  to have for the new object.
  110.  *  ppvObj          LPVOID FAR * in which to store the desired interface
  111.  *                  pointer for the new object.
  112.  *
  113.  * Return Value:
  114.  *  HRESULT         NOERROR if successful, otherwise contains E_NOINTERFACE
  115.  *                  if we cannot support the requested interface.
  116.  */
  117.  
  118. STDMETHODIMP CLinkClassFactory::CreateInstance(LPUNKNOWN punkOuter
  119.     , REFIID riid, LPVOID FAR *ppvObj)
  120.     {
  121.     LPCPatronDoc        pDoc;
  122.     HRESULT             hr;
  123.  
  124.     *ppvObj=NULL;
  125.  
  126.     hr=ResultFromScode(E_OUTOFMEMORY);
  127.  
  128.     //We don't support aggregation
  129.     if (NULL!=punkOuter)
  130.         return ResultFromScode(CLASS_E_NOAGGREGATION);
  131.  
  132.     //If NewDocument fails the doc destructor calls ObjectDestroyed.
  133.     //g_cObj++;
  134.  
  135.     //Try creating a new document, which creates the object.
  136.     pDoc=(LPCPatronDoc)m_pFR->m_pCL->NewDocument(TRUE, m_pFR->m_pAdv);
  137.  
  138.     //ObjectDestroyed has already been called.
  139.     if (NULL==pDoc)
  140.         {
  141.         g_cObj++;
  142.         ObjectDestroyed();
  143.         return hr;
  144.         }
  145.  
  146.     /*
  147.      * We don't want to do any file initialization here because we want
  148.      * to wait for IPersistFile calls.  If we called ULoad(NULL) we'd
  149.      * create storages and whatnot that we don't want in this case.
  150.      */
  151.  
  152.     hr=pDoc->QueryInterface(riid, ppvObj);
  153.  
  154.     if (FAILED(hr))
  155.         {
  156.         m_pFR->m_pCL->CloseDocument(pDoc);
  157.         g_cObj++;
  158.         ObjectDestroyed();
  159.         return hr;
  160.         }
  161.  
  162.     return NOERROR;
  163.     }
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. /*
  171.  * CLinkClassFactory::LockServer
  172.  *
  173.  * Purpose:
  174.  *  Increments or decrements the lock count of the serving IClassFactory
  175.  *  object.  When the number of locks goes to zero and the number of
  176.  *  objects is zero, we shut down the application.
  177.  *
  178.  * Parameters:
  179.  *  fLock           BOOL specifying whether to increment or decrement the
  180.  *                  lock count.
  181.  *
  182.  * Return Value:
  183.  *  HRESULT         NOERROR always.
  184.  */
  185.  
  186. STDMETHODIMP CLinkClassFactory::LockServer(BOOL fLock)
  187.     {
  188.     if (fLock)
  189.         g_cLock++;
  190.     else
  191.         {
  192.         g_cLock--;
  193.  
  194.         /*
  195.          * To centralize shutdown, we'll artificially increase the object
  196.          * count here and call ObjectDestroyed, which will decrement the
  197.          * count, see that there are no objects or locks, then shut down.
  198.          */
  199.         g_cObj++;
  200.         ObjectDestroyed();
  201.         }
  202.  
  203.     return NOERROR;
  204.     }
  205.